package com.gx.mxbbx.controller;


import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Area;
import com.gx.mxbbx.service.AreaService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 分页获取区域信息列表
     *
     * @param currentPage
     * @param limit
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getAreaList")
    public String getAreaList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String areaName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Area> pageInfo = areaService.getAllArea(areaName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", areaName);
        return "/area/area_list";
    }

    @RequestMapping("/openadd")
    public String openadd(Model model) {
        return "/area/area_add";
    }

    //添加
    @RequestMapping("/areaadd")
    public String areaadd(Area area, HttpServletRequest request, Model model) {
        areaService.areaadd(area);
        return getAreaList(1, 10, request, model);
    }

    //修改
    @RequestMapping("/areaopenEdit")
    public String areaopenEdit(Integer id, Model model) {
        Area area = areaService.getareaById(id);
        model.addAttribute("area", area);
        return "/area/area_edit";
    }

    @RequestMapping("/areaedit")
    public String areaedit(Area area, HttpServletRequest request, Model model) {
        areaService.areaedit(area);
        return getAreaList(1, 10, request, model);
    }

    @RequestMapping("/areadelete")
    public String areadelete(Integer id, HttpServletRequest request, Model model) {
        areaService.areadelete(id);
        return getAreaList(1, 10, request, model);
    }

    @ResponseBody
    @RequestMapping("/getAreaTreeList")
    public Object getAreaTreeList() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("areaList", areaService.getAreaTree(""));
        return map;
    }
}
